package homework;
//作业标题(2740)
//判断一棵二叉树是否为平衡二叉树
//
//作业内容
//判断一棵二叉树是否为平衡二叉树
//https://leetcode.cn/problems/balanced-binary-tree/description/
public class h2740 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;

        return isBalanced(root.left) && isBalanced(root.right) && Math.abs(_isBalanced(root.left) - _isBalanced(root.right)) <= 1;
    }
    private int _isBalanced(TreeNode root)
    {
        if(root == null) return 0;
        int left = _isBalanced(root.left);
        int right = _isBalanced(root.right);
        return (Math.max(left, right)) + 1;
    }

}
